<?php

/**
 * bookings actions.
 *
 * @package    playcafebookings
 * @subpackage bookings
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class bookingsActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->showPast = ($request->getParameter('show') == 'past');
    
    if ($this->showPast) {
        $this->completedBookings = Doctrine_Core::getTable('Booking')
          ->createQuery('b')
          ->where('b.status = ?', 'Completed')
          ->orderBy('b.booking_datetime DESC')
          ->execute();
    }
    else {
        $this->submittedBookings = Doctrine_Core::getTable('Booking')
          ->createQuery('b')
          ->where('b.status = ?', 'Submitted')
          ->orderBy('b.booking_datetime ASC')
          ->execute();
          
        $this->confirmedBookings = Doctrine_Core::getTable('Booking')
          ->createQuery('b')
          ->where('b.status = ?', 'Confirmed')
          ->orderBy('b.booking_datetime ASC')
          ->execute();
    }
  }
  
  public function executeToday(sfWebRequest $request)
  {
    $query = Doctrine_Core::getTable('Booking')
      ->createQuery('b')
      ->where('b.booking_datetime >= ?', date('Y-m-d 00:00:00'))
      ->andWhere('b.booking_datetime <= ?', date('Y-m-d 23:59:59'))
      ->orderBy('b.booking_datetime ASC');
      
    $this->bookings = $query->execute();
  }

  public function executeGuests(sfWebRequest $request)
  {
    $booking = Doctrine_Core::getTable('Booking')->find(array($request->getParameter('id')));
    $this->forward404Unless($booking);
    
    if ($request->getParameter('add') > 0) {
        $booking->setGuestArrivedCount($booking->getGuestArrivedCount() + $request->getParameter('add'));
    }
    else if ($request->getParameter('minus') > 0 && $booking->getGuestArrivedCount() > 0) {
        $booking->setGuestArrivedCount($booking->getGuestArrivedCount() - $request->getParameter('minus'));
    }
    
    $booking->save();

    $this->redirect('bookings/today');
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->booking = Doctrine_Core::getTable('Booking')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->booking);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new BookingForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BookingForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($booking = Doctrine_Core::getTable('Booking')->find(array($request->getParameter('id'))), sprintf('Object booking does not exist (%s).', $request->getParameter('id')));
    $this->form = new BookingForm($booking);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($booking = Doctrine_Core::getTable('Booking')->find(array($request->getParameter('id'))), sprintf('Object booking does not exist (%s).', $request->getParameter('id')));
    $this->form = new BookingForm($booking);

    $this->processForm($request, $this->form);

    $this->redirect('bookings/edit?id='.$booking->getId());
  }

  public function executeBasicUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($booking = Doctrine_Core::getTable('Booking')->find(array($request->getParameter('id'))), sprintf('Object booking does not exist (%s).', $request->getParameter('id')));

    $formValues = $request->getParameter('booking');
    if (is_array($formValues)) {
        $booking->fromArray($formValues);
        
        $customFormValues = $request->getParameter('custom');
        if (is_array($customFormValues)) {
            // The food datetime has to take the date from the booking datetime
            $bookingDatetime = strtotime($booking->getBookingDatetime());
            $bookingDate = date('Y-m-d', $bookingDatetime);
            $booking->setFoodDatetime(date('Y-m-d H:i:s', strtotime($bookingDate . ' ' . $customFormValues['food_datetime_time'])));
        }
        
        $booking->save();
    }

    $this->getResponse()->setContentType('application/json');
    return $this->renderText(json_encode(array('success' => true)));
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($booking = Doctrine_Core::getTable('Booking')->find(array($request->getParameter('id'))), sprintf('Object booking does not exist (%s).', $request->getParameter('id')));
    $booking->delete();

    $this->redirect('bookings/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $customValues = $request->getParameter('custom');
    
    $form->bindWithCustomValues($request->getParameter($form->getName()), $request->getFiles($form->getName()), $customValues);
    if ($form->isValid())
    {
      $booking = $form->save();

      $this->redirect('bookings/index');
    }
  }
}
